﻿using System.Collections.Generic;
using System.Linq;
using TimeMachine.Engine.Vcs;

namespace TimeMachine.Engine.Reports
{
	public class WorstCodeReport
	{
		private readonly TimeRange timeRange;
		private readonly IChangesProcessor changesProcessor;
		private readonly ICommitsInfoProvider commits;
		private readonly WorstCodeStatsCalculator calculator;

		public WorstCodeReport(TimeRange timeRange, IChangesProcessor changesProcessor,
			ICommitsInfoProvider commits, WorstCodeStatsCalculator calculator)
		{
			this.timeRange = timeRange;
			this.changesProcessor = changesProcessor;
			this.commits = commits;
			this.calculator = calculator;
		}

		public IEnumerable<FileStat> Calculate()
		{
			foreach(var commit in commits.Get().Where(timeRange.Containes))
			{
				DetailedCommitInfo theCommit = commit;
				changesProcessor.ProcessChanges(
					commit.Revision,
					new ProcessChangesParams
						{
							OnNextFile = filename => calculator.OnNextFile(filename, theCommit),
							OnNextChange = calculator.OnNextChange
						}
					);
			}
			return calculator.GetStats(100);
		}
	}
}